#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2023/3/21 18:50
# @Author  : huidong.bai
# @File    : parse_config.py
# @Software: PyCharm
# @Mail    : MasterBai2018@outlook.com


import pdb
import copy
import sys
import os
import subprocess
import json
import string
import re
import datetime
from enum import Enum


class MongoConfig:
    emailReceiver = []
    emailTitle = None
    emailContent = None
    mongoConfig = None
    testDataDate = None
    emailOption = None

    def __init__(self, mongoConfig):
        self.reset()
        self.mongoConfig = mongoConfig
        self.parse()

    def reset(self):
        self.emailReceiver = None
        self.emailTitle = None
        self.emailContent = None
        self.testDataDate = None
        self.emailOption = None

    def get_emailOption(self):
        return self.emailOption

    def get_emailReceiver(self):
        return self.emailReceiver

    def get_emailTitle(self):
        return self.emailTitle

    def get_emailContent(self):
        return self.emailContent

    def getTestDataDate(self):
        return self.testDataDate

    def isDateMatch(self, caselist):
        if self.testDataDate is None:
            return True
        caseDate = caselist.split("/")[-1][0:10]
        if len(caseDate.split("-")) >= 3:
            beginDate = self.testDataDate.split("~")[0]
            endDate = self.testDataDate.split("~")[1]
            difference = self.__getDifference(beginDate, caseDate)
            difference_ = self.__getDifference(caseDate, endDate)
            if difference >= 0 and difference_ >= 0:
                return True
        return False

    def __getDifference(self, begin_date, end_date):
        date1 = datetime.datetime.strptime(begin_date, "%Y-%m-%d")
        date2 = datetime.datetime.strptime(end_date, "%Y-%m-%d")
        difference = (date2 - date1).days
        return difference

    def parse(self):
        try:
            with open(self.mongoConfig, 'r') as fp:
                lines = fp.readlines()
        except Exception as e:
            print(f'Error: {e} : The path [{self.mongoConfig}] is not exists')
        else:
            for line in lines:
                data = line.replace('\n', '')
                if '#' in data:
                    continue
                if 'RECEIVER' in data:
                    emailReceiver = data.split('=')[-1]
                    for i in range(0, len(emailReceiver)):
                        if emailReceiver[i] != ' ':
                            self.emailReceiver = emailReceiver[i:].replace(' ', '').split(',')
                            break
                if 'TITLE' in data:
                    emailTitle = data.split('=')[-1]
                    for j in range(0, len(emailTitle)):
                        if emailTitle[j] != ' ':
                            self.emailTitle = emailTitle[j:]
                            break
                if 'CONTENT' in data:
                    emailContent = data.split('=')[-1]
                    for k in range(0, len(emailContent)):
                        if emailContent[k] != ' ':
                            self.emailContent = emailContent[k:]
                            break
                if 'TEST_DATA_DATE' in data:
                    testDataDate = data.split('=')[-1]
                    for l in range(0, len(testDataDate)):
                        if testDataDate[l] != ' ':
                            self.testDataDate = testDataDate[l:]
                            if "~" not in self.testDataDate:
                                currentDate = datetime.datetime.now().strftime('%Y-%m-%d')
                                currentYear = currentDate.split("-")[0]
                                currentMonth = currentDate.split("-")[1]
                                month = int(currentMonth) - int(self.testDataDate)
                                if month <= 0:
                                    month_ = month + 12
                                    year = int(currentYear) - 1
                                    self.testDataDate = currentDate.replace(currentMonth, '0' + str(month_)).replace(
                                        currentYear, str(year))
                                elif month > 0:
                                    self.testDataDate = currentDate.replace(currentMonth, '0' + str(month))
                                self.testDataDate += '~' + currentDate
                            break
                if 'EMAIL_OPTION' in data:
                    emailOption = data.split('=')[-1]
                    for m in range(0, len(emailOption)):
                        if emailOption[m] != ' ':
                            self.emailOption = emailOption[m:]
                            break


class AssertLevel(Enum):
    SCENARIO = 0
    SUITE = 1
    CASE = 2


class Assertion:
    level = AssertLevel.CASE
    info = None

    def __init__(self, level, info):
        self.level = level
        self.info = info
        return


class Suite:
    suiteCounter = 0
    suiteID = 0
    name = None
    conf = None
    tag = None
    caseList = None
    scenario_conf = None
    repeatNum = 1
    realTime = False
    abstract = None
    assertion = None

    def __init__(self, ID, name, conf, _tag, caseList):
        # pdb.set_trace()
        self.suiteID = ID
        self.name = name
        self.conf = conf
        if _tag is not None:
            # self.tag = copy.deepcopy(_tag.replace('[', '').split(']')[0:-1])
            self.tag = _tag
        self.caseList = caseList
        Suite.suiteCounter += 1
        self.assertion = None

    def set_repeatNum(self, number):
        self.repeatNum = number

    def set_isRealTime(self, flag):
        if flag.lower() == "true":
            self.isRealTime = True
        else:
            self.isRealTime = False

    def set_abstract(self, data):
        self.abstract = data

    def set_scenarioConfig(self, config):
        self.scenario_conf = config

    def set_assertion(self, assertion):
        self.assertion = Assertion(AssertLevel.SUITE, assertion)


class Scenario:
    scenarioCounter = 0
    scenarioID = 0
    suiteNum = 0
    curSuiteNum = 0
    suiteInfo = []
    curSuite = None
    stdoutFile = None
    isParallel = False
    useValgrind = False
    config = None
    repeatNum = 1
    assertion = None
    owner = None

    def __init__(self, name):
        self.name = name
        self.suiteNum = 0
        self.curSuiteNum = 0
        self.suiteInfo = []
        self.curSuite = None
        self.stdoutFile = None
        Scenario.scenarioCounter += 1
        self.scenarioID = Scenario.scenarioCounter
        self.assertion = None
        self.owner = None

    def set_useValgrind(self, flag):
        if flag.lower() == "true":
            self.useValgrind = True
        elif flag.lower() == "false":
            self.useValgrind = False
        else:
            self.useValgrind = False
            print(f"the parameter:{flag} of 'VALGRIND' in config:{self.config} is error")

    def set_isParallel(self, flag):
        if flag.lower() == "true":
            self.isParallel = True
        elif flag.lower() == "false":
            self.isParallel = False
        else:
            self.isParallel = False
            print("the parameter:%s of 'PARALLEL' in config:%s is error" % flag, self.config)

    def set_repeatNum(self, number):
        self.repeatNum = number

    def set_purposeData(self, data):
        self.purposeData = data

    def set_stdoutFile(self, stdoutFile):
        self.stdoutFile = stdoutFile

    def set_suite(self, suiteName, suiteConf, suiteTag, caseList):
        # pdb.set_trace()
        if self.curSuiteNum != 0:
            self.suiteInfo.append(self.curSuite)
        self.curSuiteNum += 1
        self.curSuite = Suite(self.curSuiteNum, suiteName, suiteConf, suiteTag, caseList)
        self.suiteNum = Suite.suiteCounter

    def set_suiteParam(self, param, data):
        if param == "ARG_REPEAT":
            self.curSuite.set_repeatNum(int(data))
        elif param == "ARG_PARALLEL":
            self.curSuite.set_isRealTime(data)
        elif param == "ARG_ABSTRACT":
            self.curSuite.set_abstract(data)
        elif param == "ARG_ASSERT":
            self.curSuite.set_assertion(data)

    def set_scenarioConf(self, config):
        self.config = config

    def set_scenarioOwner(self, owner):
        self.owner = owner.split(',')[:]

    def set_scenarioAssertion(self, assertion):
        self.assertion = Assertion(AssertLevel.SCENARIO, assertion)

    def end(self):
        if self.curSuite is None:
            return
        self.suiteInfo.append(self.curSuite)
        for suite in self.suiteInfo:
            suite.set_scenarioConfig(self.config)


class ConcurrentCtr:
    pid = 0
    totalProNum = 0
    onlineNum = 0
    cpNum = 0
    othersNum = 0

    def __init__(self, pid, info):
        self.pid = pid
        if info is None:
            totalProNum = 0
            onlineNum = 0
            cpNum = 0
            othersNum = 0
            return
        infoList = info.split(',')
        for _info in infoList:
            if 'CP' in _info:
                cpNum = int(_info.split('-')[1])
                if cpNum < 0:
                    cpNum = 0
                self.cpNum = cpNum
            elif 'ONLINE' in _info:
                onlineNum = int(_info.split('-')[1])
                if onlineNum < 0:
                    onlineNum = 0
                self.onlineNum = onlineNum
            elif 'OTHER' in _info:
                othersNum = int(_info.split('-')[1])
                if othersNum < 0:
                    othersNum = 0
                self.othersNum = othersNum
            else:
                print("%s maybe error\n" % _info)
        self.totalProNum = self.onlineNum + self.cpNum + self.othersNum

    def updateInfo(self, pid, cpNum, onlineNum, othersNum):
        self.pid = pid
        self.cpNum = cpNum
        self.onlineNum = onlineNum
        self.othersNum = othersNum
        self.totalProNum = cpNum + onlineNum + othersNum


class Parse:
    user = None
    url = None
    jenkins = []
    needLoad = False
    needCleanWorkSpace = True
    workspace = None
    concurrentCtr = None
    dataVersion = None
    pstt_server = None
    django_server = None
    sceneList = []
    enable = []
    disable = []
    tag = []
    needfullTag = []
    solutionName = None
    mongoConfig = None
    entityConfig = None
    keyWords = ['DEFINE', 'NAME', 'CONFIG', 'SUITE', 'REPEAT', 'PARALLEL', 'VALGRIND', 'PURPOSE', 'ARG_PARALLEL',
                'ARG_REPEAT', 'ARG_ASSERT', 'END', '@']
    sceneParam = ['CONFIG', 'REPEAT', 'PARALLEL', 'PURPOSE', 'STDOUT', 'VALGRIND', 'ASSERT']
    suiteParam = ['ARG_PARALLEL', 'ARG_REPEAT']

    def __init__(self, cfgFile, _tag, _enable, work):
        self.reset()
        self.workspace = work
        self.config = cfgFile
        # pdb.set_trace()
        if _tag is not None:
            self.tag = copy.deepcopy(_tag.replace('[', '').split(']')[0:-1])
            for tag_ in self.tag:
                if '+' in tag_:
                    self.needfullTag.append(tag_[1:])
        if _enable is not None:
            self.enable = _enable.split(',')
        self.solutionName = self.extractSolutionName(self.config)
        self.parse()

    def extractSolutionName(self, config):
        if config is not None:
            return config.split('/')[-1]
        return None

    def reset(self):
        self.user = None
        self.url = None
        self.jenkins = []
        self.needLoad = False
        self.needCleanWorkSpace = True
        self.workspace = None
        self.concurrentCtr = None
        self.dataVersion = None
        self.pstt_server = None
        self.django_server = None
        self.sceneList = []
        self.enable = []
        self.disable = []
        self.tag = []
        self.needfullTag = []
        self.solutionName = None
        self.entityConfig = None
        self.mongoConfig = None

    def get_mongoConfig(self):
        if self.entityConfig is None:
            self.entityConfig = MongoConfig(self.mongoConfig)
        return self.entityConfig

    def get_enable(self):
        return self.enable

    def get_disable(self):
        return self.disable

    def get_tasks(self):
        return self.sceneList

    def get_needLoad(self):
        return self.needLoad

    def get_needCleanWorkSpace(self):
        return self.needCleanWorkSpace

    def get_concurrentCtr(self, pid):
        return ConcurrentCtr(pid, self.concurrentCtr)

    def get_user(self):
        return self.user

    def get_url(self):
        return self.url

    def get_jenkins(self):
        return self.jenkins

    def get_pstt_serverInfo(self):
        return self.pstt_server

    def get_django_serverInfo(self):
        return self.django_server

    def setConcurrentCtr(self, info):
        self.concurrentCtr = info

    def setDataVersion(self, version):
        self.dataVersion = version

    def getOwner(self, scenario):
        for s in self.sceneList:
            if s.name == scenario:
                return s.owner
        return None

    def getScenarioAssertion(self, scenarioName):
        for scenario in self.sceneList:
            if scenario.name == scenarioName:
                return scenario.assertion
        return None

    def getSuiteAssertion(self, scenarioName, suiteID):
        for scenario in self.sceneList:
            if scenario.name == scenarioName:
                for suite in scenario.suiteInfo:
                    if suite.suiteID == suiteID:
                        return suite.assertion
        return None

    def getSuiteCaseList(self, scenarioName, suiteID):
        for scenario in self.sceneList:
            if scenario.name == scenarioName:
                for suite in scenario.suiteInfo:
                    if suite.suiteID == suiteID:
                        if suite.caseList is not None:
                            return suite.caseList.split('/')[-1]
        return None

    def getMatchFile(self, dirName, fileRegular):
        matchedFile = []
        for root, dirs, files in os.walk(dirName):
            for file_ in files:
                if re.match(fileRegular, file_):
                    fileName = os.path.join(root, file_)
                    matchedFile.append(fileName)
        return matchedFile

    def checkMongoVersion(self, workspace, tools):
        if self.dataVersion is None:
            print("    The data's version is None !\n    Configure version information in secenario_config, like this:")
            print("        BOLOO_VERSION : EQ mongo-x.x.x, or")
            print("        BOLOO_VERSION : GT mongo-x.x.x, or")
            print("        BOLOO_VERSION : GE mongo-x.x.x")
            return False
        for tool in tools:
            subprocess.call("%s/%s --mongo_work_space=%s --mongo_version" % (workspace, tool, workspace), shell=True)
            try:
                with open("%s/version" % workspace, 'r') as fp:
                    data = fp.read()
                    try:
                        version_info = json.loads(data, encoding="utf-8")
                    except:
                        data = data.decode("gbk").encode("utf-8")
                        version_info = json.loads(data, encoding="utf-8")
            except IOError:
                print("%s/version does not exist." % workspace)
            else:
                version = version_info["version"]
                print("the current Mongo version:%s the data's version:%s" % (version, self.dataVersion))
                # pdb.set_trace()
                MongoVersion = version.split('.')
                dataVersion = self.dataVersion.split('-')[1].split('.')
                fp.close()
                digitLen = len(MongoVersion)
                if digitLen != len(dataVersion):
                    return False
                MongoVersion_ = int(MongoVersion[0]) * 100 + int(MongoVersion[1]) * 10 + int(MongoVersion[2])
                dataVersion_ = int(dataVersion[0]) * 100 + int(dataVersion[1]) * 10 + int(dataVersion[2])
                if 'EQ' in self.dataVersion:
                    if MongoVersion_ != dataVersion_:
                        return False
                elif 'GT' in self.dataVersion:
                    if MongoVersion_ <= dataVersion_:
                        return False
                elif 'GE' in self.dataVersion:
                    if MongoVersion_ < dataVersion_:
                        return False
                else:
                    return False
        return True

    def scanInfo(self, scenario, suiteInfoLen, suiteInfo, index, data, line):
        if suiteInfoLen < 3:
            print("ERROR : File '%s', line '%d', %s\nYou must give four params at least\n \
                  'SCAN : suitename,config,caselistdir,regular'" % (self.config, index + 1, data))
            sys.exit(0)
        else:
            matched = False
            suiteTag = None
            suiteCaseList = None
            suiteRegular = None
            caseDir = suiteInfo[2]
            suiteCaseLists = []
            if suiteInfoLen == 4:
                if '[' in suiteInfo[3] and ']' in suiteInfo[3]:
                    suiteTag = suiteInfo[3]
                elif '<' in suiteInfo[3] and '>' in suiteInfo[3]:
                    suiteRegular = suiteInfo[3]
                else:
                    print(
                        "ERROR : File '%s', line '%d', %s\nThe tag format must be '[XX]'." % (self.config, index, data))
            elif suiteInfoLen == 5:
                if '[' in suiteInfo[3] and ']' in suiteInfo[3]:
                    suiteTag = suiteInfo[3]
                elif '<' in suiteInfo[3] and '>' in suiteInfo[3]:
                    suiteRegular = suiteInfo[3]
                else:
                    print(
                        "ERROR : File '%s', line '%d', %s\nThe tag format must be '[XX]'." % (self.config, index, data))
                if '[' in suiteInfo[4] and ']' in suiteInfo[4]:
                    suiteTag = suiteInfo[4]
                elif '<' in suiteInfo[4] and '>' in suiteInfo[4]:
                    suiteRegular = suiteInfo[4]
                else:
                    print(
                        "ERROR : File '%s', line '%d', %s\nThe tag format must be '[XX]'." % (self.config, index, data))
            if len(self.tag) > 0:
                if suiteTag is None:
                    index += 1
                    return index
                suiteTagList = suiteTag.replace('[', '').split(']')[0:1]
                if '+' in suiteTagList:
                    matched = True
                elif '-' in suiteTagList:
                    matched = False
                else:
                    if len(self.needfullTag) > 0:
                        matched = True
                        for _tag in self.needfullTag:
                            if _tag not in suiteTagList:
                                matched = False
                                break
                    else:
                        for _tag in self.tag:
                            if _tag in suiteTagList:
                                matched = True
                                break
            else:
                matched = True
            if matched:
                ARG = None
                index += 1
                if index <= line[index] and 'ARG_' in line[index]:
                    ARG = line[index]
                if suiteRegular is None:
                    suiteRegular_ = ''
                else:
                    suiteRegular_ = suiteRegular.replace('<', '').replace('>', '')
                try:
                    suiteCaseLists = self.getMatchFile(caseDir, suiteRegular_)
                except Exception as e:
                    print(f"Error:{e}, please check the match regular!")
                else:
                    for suiteCaseList in suiteCaseLists:
                        if self.mongoConfig is not None:
                            matched = self.mongoConfig.isDateMatch(suiteCaseList)
                        if matched:
                            scenario.set_suite(suiteInfo[0], suiteInfo[1], suiteTag, suiteCaseList)
                        else:
                            continue
                        if ARG is not None:
                            suiteParam = ARG.split(':')
                            scenario.set_suiteParam(suiteParam[0], suiteParam[1])
            else:
                index += 1
                if 'ARG' in line[index]:
                    index += 1
                return index
        index += 1
        return index

    def PRINT(self):
        log = ""
        if os.path.exists(self.workspace) is False:
            os.makedirs(self.workspace)
        log = "%s/Mongo.log" % self.workspace
        with open(log, 'w') as file_log:
            for SCENARIO in self.sceneList:
                file_log.write("SCENARIO NAME:%s  CONF:%s\n" % (SCENARIO.name, SCENARIO.config))
                for SUITE in SCENARIO.suiteInfo:
                    file_log.write(
                        "    suite_id:%d  suite_name:%s  suite_tag:%s  suite_config:%s  suite_caseList:%s\n" % (
                        SUITE.suiteID, SUITE.name, SUITE.tag, SUITE.conf, SUITE.caseList))
                file_log.write("\n\n")

    def parse(self):
        try:
            with open(self.config, 'r') as fp:
                _line = fp.readlines()
        except IOError:
            print("The solution file : %s does not exist." % self.config)
            exit(0)
        else:
            '''
            for i in range(0, len(line)):
                data = line[i].expandtabs(8).strip().replace(' ', '').split('@')[0]
                line[i] = data
            '''
            absIndex = 0
            absPos = []
            line = []
            for line_ in _line:
                # remove the unnecessary indents, spaces and comments
                data = line_.expandtabs(8).strip().replace(' ', '').split('@')[0]
                absIndex += 1
                if len(data) > 0:
                    line.append(data)
                    absPos.append(absIndex)
            index = 0
            flag = 0
            while index < len(line):
                # pdb.set_trace()
                data = line[index]
                if 'DEFINE' not in data and '@' not in data:
                    if "USER" in data:
                        self.user = data.split(':')[1].replace('/', ':')
                        index += 1
                        continue
                    if "URL" in data:
                        _url = data.split(':')
                        for i in range(0, len(_url)):
                            if i == 1:
                                self.url = _url[i]
                            elif i > 1:
                                self.url += ":%s" % _url[i]
                        index += 1
                        continue
                    if "JENKINS" in data:
                        self.jenkins = data.split(':')[1].split(',')
                        index += 1
                        continue
                    if "NEEDLOAD" in data:
                        if "YES" in data or "yes" in data:
                            self.needLoad = True
                        index += 1
                        continue
                    if "CLEAN_BOLOO_WORKSPACE" in data:
                        if "NO" in data or "no" in data:
                            self.needCleanWorkSpace = False
                        index += 1
                        continue
                    if "CONCURRENT_CONTROLLER" in data:
                        self.setConcurrentCtr(data.split(':')[1])
                        index += 1
                        continue
                    if "PSTT_SERVER" in data:
                        self.pstt_server = data.split(':')[1]
                        index += 1
                        continue
                    if "DJANGO_SERVER" in data:
                        self.django_server = data.split(':')[1]
                        index += 1
                        continue
                    if "BOLOO_VERSION" in data:
                        self.setDataVersion(data.split(':')[1])
                        index += 1
                        continue
                    if "ENABLE" in data:
                        if len(self.enable) == 0:
                            self.enable = data.split(':')[1].split(',')
                        index += 1
                        continue
                    if "DISABLE" in data:
                        self.disable = data.split(':')[1].split(',')
                        index += 1
                        continue
                    if "CONFIG" in data:
                        self.mongoConfig = data.split(':')[1]
                        self.mongoConfig = self.get_mongoConfig()
                        index += 1
                        continue
                    if 0 == len(data):
                        index += 1
                        continue
                    for keyword in self.keyWords:
                        if keyword in data:
                            print(
                                f"ERROR : File '{self.config}', line '{index + 1}', {data}\n\tThe '{keyword}' is a keyWord in Mongo, it must be used between 'DEFINE' and 'END'.")
                            sys.exit(0)
                    print("WARING : File '%s', line '%d', %s" % self.config, absPos[index], data)
                    index += 1
                    continue
                # pdb.set_trace()
                if 'DEFINE' in data:
                    flag = 1
                    if len(data) > len("DEFINE"):
                        print(
                            "WARING : File '%s', line '%d', %s\n\tThere should be nothing behind 'DEFINE'." % self.config,
                            index + 1, data)
                    index += 1
                    data = line[index]
                    if 'NAME:' not in data:
                        print(
                            f"ERROR : File '{self.config}', line '{index + 1}', {data}\n\tYou must give the name of the current scene when you defined scene test.")
                        sys.exit(0)
                    sceneName = data.split(':')[1]
                    scenario = Scenario(sceneName)
                    index += 1
                    while index < len(line):
                        data = line[index]
                        if 0 == len(data):
                            index += 1
                            continue
                        if 'SCAN' in data:
                            # pdb.set_trace()
                            suiteInfo = data.split(':')[1].split(',')
                            suiteInfoLen = len(suiteInfo)
                            index = self.scanInfo(scenario, suiteInfoLen, suiteInfo, index, data, line)
                            continue
                        if 'SUITE:' in data:
                            # pdb.set_trace()
                            suiteInfo = data.split(':')[1].split(',')
                            suiteInfoLen = len(suiteInfo)
                            # print suiteInfo[0],suiteInfo[1]
                            if suiteInfoLen < 2:
                                print(
                                    "ERROR : File '%s', line '%d', %s\n\tThere must be ',' between suite and config'." % self.config,
                                    index + 1, data)
                                sys.exit(0)
                            else:
                                matched = False
                                suiteTag = None
                                suiteCaseList = None
                                if len(self.tag) > 0:
                                    # pdb.set_trace()
                                    if suiteInfoLen == 2:
                                        index += 1
                                        continue
                                    elif suiteInfoLen == 3:
                                        if '[' in suiteInfo[2] and ']' in suiteInfo[2]:
                                            suiteTag = suiteInfo[2]
                                        else:
                                            suiteCaseList = suiteInfo[2]
                                    else:
                                        if '[' in suiteInfo[2] and ']' in suiteInfo[2]:
                                            suiteTag = suiteInfo[2]
                                        if suiteTag is None:
                                            if '[' in suiteInfo[3] and ']' in suiteInfo[3]:
                                                suiteTag = suiteInfo[3]
                                        if suiteTag is not suiteInfo[2]:
                                            suiteCaseList = suiteInfo[2]
                                        else:
                                            suiteCaseList = suiteInfo[3]

                                    if suiteTag is None:
                                        index += 1
                                        continue

                                    suiteTagList = suiteTag.replace('[', '').split(']')[0:-1]

                                    # +在标签中，表示必须跑的suite
                                    if '+' in suiteTagList:
                                        matched = True
                                    # -在标签中，表示一定不会跑的suite
                                    elif '-' in suiteTagList:
                                        matched = False
                                    else:
                                        if len(self.needfullTag) > 0:
                                            matched = True
                                            for _tag in self.needfullTag:
                                                if _tag not in suiteTagList:
                                                    matched = False
                                                    break
                                        else:
                                            for _tag in self.tag:
                                                if _tag in suiteTagList:
                                                    matched = True
                                                    break
                                else:
                                    matched = True

                                    if suiteInfoLen == 2:
                                        suiteTag = None
                                        suiteCaseList = None
                                    elif suiteInfoLen == 3:
                                        if '[' in suiteInfo[2] and ']' in suiteInfo[2]:
                                            suiteTag = suiteInfo[2]
                                        else:
                                            suiteCaseList = suiteInfo[2]
                                    else:
                                        if '[' in suiteInfo[2] and ']' in suiteInfo[2]:
                                            suiteTag = suiteInfo[2]
                                        if suiteTag is None:
                                            if '[' in suiteInfo[3] and ']' in suiteInfo[3]:
                                                suiteTag = suiteInfo[3]
                                        if suiteTag is not suiteInfo[2]:
                                            suiteCaseList = suiteInfo[2]
                                        else:
                                            suiteCaseList = suiteInfo[3]

                                if matched:
                                    if self.mongoConfig is not None:
                                        matched = self.mongoConfig.isDateMatch(suiteCaseList)
                                    if matched:
                                        scenario.set_suite(suiteInfo[0], suiteInfo[1], suiteTag, suiteCaseList)
                                    else:
                                        index += 1
                                        continue
                                else:
                                    index += 1
                                    # print "the current suite's tag doesn't match the label"
                                    continue
                            index += 1
                            if index < len(line):
                                data = line[index]
                                while 'ARG_' in data:
                                    # pdb.set_trace()
                                    suiteParam = data.split(':')
                                    scenario.set_suiteParam(suiteParam[0], suiteParam[1])
                                    index += 1
                                    if index < len(line):
                                        data = line[index]
                                        continue
                                    else:
                                        break
                            else:
                                break
                        elif 'CONFIG:' in data or 'REPEAT:' in data or 'PARALLEL:' in data or 'PURPOSE:' in data or 'STDOUT:' in data or 'VALGRIND:' in data or 'OWNER' in data or (
                                "ASSERT" in data and "ARG_ASSERT" not in data):
                            if 'REPEAT:' in data:
                                scenario.set_repeatNum(int(data.split(':')[1]))
                                index += 1
                            elif 'PARALLEL:' in data:
                                scenario.set_isParallel(data.split(':')[1])
                                index += 1
                            elif 'PURPOSE:' in data:
                                scenario.set_purposeData(data.split(':')[1])
                                index += 1
                            elif 'STDOUT:' in data:
                                scenario.set_stdoutFile(data.split(':')[1])
                                index += 1
                            elif 'VALGRIND:' in data:
                                scenario.set_useValgrind(data.split(':')[1])
                                index += 1
                            elif 'CONFIG:' in data:
                                scenario.set_scenarioConf(data.split(':')[1])
                                index += 1
                            elif 'OWNER' in data:
                                scenario.set_scenarioOwner(data.split(':')[1])
                                index += 1
                            elif 'ASSERT' in data:
                                scenario.set_scenarioAssertion(data.split(':')[1])
                                index += 1
                        elif 'END' in data:
                            # pdb.set_trace()
                            scenario.end()
                            self.sceneList.append(scenario)
                            flag = 2
                            break
                        else:
                            print("WARING : File '%s', line '%d', %s" % (self.config, absPos[index], data))
                            index += 1
                    index += 1
            if flag == 0:
                print("ERROR : Nothing in file '%s', " % self.config)
                sys.exit(0)
            elif flag == 1:
                print(
                    f"ERROR : File '{self.config}', line '{index}', {data}\n\tYou must give 'END' at the end the current scene.")
                sys.exit(0)
            elif flag == 2:
                print("parse the solution file: '%s'  complete." % self.config)
        fp.close()
        self.PRINT()
        # return self.sceneList
